package logic

import (
	"context"
	"errors"
	"strings"
	"time"
	"zhongwen/cache"
	"zhongwen/models/mysql"

	"zhongwen/zhongwenrpc/internal/svc"
	"zhongwen/zhongwenrpc/zhongwenrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *zhongwenrpc.CommentAddRequest) (*zhongwenrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var novel mysql.Novel
	mysql.DB.Debug().Where("id=?", in.NovelId).Find(&novel)
	if novel.ID == 0 {
		return nil, errors.New("当前小说不存在")
	}
	if strings.Contains(in.Context, "抄袭") || strings.Contains(in.Context, "涉政") {
		return nil, errors.New("包含敏感词汇发送失败")
	}
	comment := mysql.Comment{
		UserId:  in.UserId,
		NovelId: in.NovelId,
		Context: in.Context,
	}
	mysql.DB.Debug().Create(&comment)
	cache.Rdb.Set(cache.Ctx, "sendComment"+in.Context, nil, time.Minute*15)
	return &zhongwenrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
